package com.hdc.mumuwei.micro_account_server.service.impl;

import com.hdc.mumuwei.micro_account_server.mapper.UserMapper;
import com.hdc.mumuwei.micro_account_server.po.Paging;
import com.hdc.mumuwei.micro_account_server.po.User;
import com.hdc.mumuwei.micro_account_server.service.UserService;
import com.hdc.mumuwei.micro_account_server.utils.PassUtils;
import com.hdc.mumuwei.micro_account_server.utils.Result;
import com.hdc.mumuwei.micro_account_server.utils.TimeUtils;
import com.hdc.mumuwei.micro_account_server.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mumuwei
 * @Description
 * @create 2021/11/19 22:47
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    Result result;

    /**
     * 注册用户
     *
     * @param user 注册用户信息
     * @return 注册成功提示
     */
    @Override
    public Result registerUser(User user) {
        user.setType(0);
        user.setPassword(PassUtils.createMD5(user.getPassword()));
        user.setCreateTime(TimeUtils.getCurrentTimes());
        if (userMapper.findUserByUsername(user.getUsername()) == null) {
            if (userMapper.addUser(user) > 0) {
                result = new Result<>(Result.REGISTER_OK_MSG);
            } else {
                result = new Result<>(Result.ERR_CODE,Result.REGISTER_ERR_MSG);
            }
        } else {
            result = new Result<>(Result.ERR_EXIST_CODE,Result.USER_EXIST_MSG);
        }
        return result;
    }

    /**
     * 登录
     *
     * @param user 用户名，密码
     * @return 登录用户的个人信息
     */
    @Override
    public Result login(User user) {
        user.setPassword(PassUtils.createMD5(user.getPassword()));
        User find = userMapper.findUserByUsername(user.getUsername());
        if(find == null){
            result = new Result<>(Result.ERR_EXIST_CODE,Result.USER_NO_EXIST_MSG);
        }else {
            String password = userMapper.findUserPassByUsername(find.getUsername());
            if(password.equals(user.getPassword())){
                find.setToken(TokenUtil.generateToken(find.getId().toString()));
                result = new Result<>(Result.LOGIN_OK_MSG,find);
            }else {
                result = new Result<>(Result.ERR_CODE,Result.LOGIN_ERR_MSG);
            }
        }
        return result;
    }

    /**
     * 微信登录
     *
     * @param openId
     */
    @Override
    public Result loginWeixin(String openId) {
        User find = userMapper.findUserByOpenId(openId);
        if(find == null){
            result = new Result<>(Result.ERR_EXIST_CODE,Result.USER_NO_EXIST_MSG);
        }else {
            find.setToken(TokenUtil.generateToken(find.getId().toString()));
            result = new Result<>(Result.LOGIN_OK_MSG,find);
        }
        return result;
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param id 用户id
     * @return 用户对象
     */
    @Override
    public Result getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        if(user != null){
            result = new Result(Result.SUCCESS_CODE,Result.SUCCESS_MSG,user);
        }else {
            result = new Result(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 修改密码
     *
     * @param user 用户id，密码，新密码
     * @return 修改成功提示
     */
    @Override
    public Result editPass(User user) {
        user.setPassword(PassUtils.createMD5(user.getPassword()));
        String password = userMapper.findUserPassByid(user.getId());
        if(password.equals(user.getPassword())){
            String newPass = user.getNewPass();
            newPass = PassUtils.createMD5(newPass);
            user.setPassword(newPass);
            user.setUpdateTime(TimeUtils.getCurrentTimes());
            int row = userMapper.editPassword(user);
            if(row>0){
                result = new Result<>(Result.SUCCESS_CODE, Result.SUCCESS_MSG);
            }else {
                result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
            }
        } else {
            result = new Result<>(Result.ERR_CODE, Result.PASS_ERR_MSG);
        }
        return result;
    }

    /**
     * 通过邮箱重置密码
     *
     * @param email 用户邮箱
     * @return 请求成功
     */
    @Override
    public Result resetPassByEmail(String email) {
        return null;
    }

    /**
     * 用户修改本人信息
     *
     * @param user 用户更新信息
     * @return 操作成功
     */
    @Override
    public Result editUserInfo(User user) {
        user.setType(0);
        user.setUpdateTime(TimeUtils.getCurrentTimes());
        int row = userMapper.updateUserInfo(user);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.UPDATE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 验证是否本人邮箱
     *
     * @param email 邮箱
     * @return 操作成功
     */
    @Override
    public Result confirmEmail(String email) {
        return null;
    }

    /**
     * 管理员添加用户
     *
     * @param user 注册用户信息
     * @return 添加成功提示
     */
    @Override
    public Result addUser(User user) {
        user.setPassword(PassUtils.createMD5(user.getPassword()));
        User find = userMapper.findUserByUsername(user.getUsername());
        if( find == null){
            user.setCreateTime(TimeUtils.getCurrentTimes());
            int row = userMapper.addUser(user);
            if(row>0){
                result = new Result<>(Result.SUCCESS_CODE, Result.ADD_OK_MSG);
            }else {
                result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
            }
        }else {
            result = new Result<>(Result.ERR_EXIST_CODE, Result.USER_EXIST_MSG);
        }
        return result;
    }

    /**
     * 管理员修改用户信息
     *
     * @param user 用户信息
     * @return 操作成功
     */
    @Override
    public Result editUserInfoByAdmin(User user) {
        Result result;
        user.setUpdateTime(TimeUtils.getCurrentTimes());
        int row = userMapper.updateUserInfo(user);
        if(row>0){
            result = new Result(Result.SUCCESS_CODE, Result.UPDATE_OK_MSG);
        }else {
            result = new Result(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 管理员删除用户
     *
     * @param id 被用户id
     * @return 操作成功
     */
    @Override
    public Result delUser(Integer id) {
        int row = userMapper.delUserById(id);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.DELETE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 管理员重置密码
     *
     * @param user 用户id,新密码
     * @return 操作成功
     */
    @Override
    public Result resetPassByAdmin(User user) {
        user.setPassword(PassUtils.createMD5(user.getPassword()));
        int row = userMapper.editPassword(user);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.UPDATE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 获取用户列表(不分页)
     *
     * @return 用户列表（不分页）
     */
    @Override
    public Result getAllUsers() {
        Map<String, Object> data = new HashMap();
        int total = userMapper.getUserTotal(null);
        data.put("total", total);
        List list = userMapper.getAllUsers();
        data.put("list", list);
        if(list!= null){
            result = new Result<>(data);
        }else {
            result = new Result<>(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 获取用户列表（分页）
     *
     * @param paging 分页传参对象
     * @return 用户列表(分页)
     */
    @Override
    public Result getUserList(Paging paging) {
        Map<String, Object> data = new HashMap();
        paging.setStart((paging.getCurrent()-1)* paging.getSize());
        int total = userMapper.getUserTotal(paging);
        data.put("total", total);
        List list = userMapper.getUserList(paging);
        data.put("list", list);
        if(list!= null){
            result = new Result<>(data);
        }else {
            result = new Result<>(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }
}
